package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import util.JwtUtil;
import util.MD5Util;
import util.Result;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@ResponseBody
public class TestController {

    @RequestMapping("/test.do")
    public Object test(String token){
        //System.out.println(1/0);
        System.out.println("test");

        //String token = JwtUtil.createJwt();
        //System.out.println(token);

//        System.out.println(MD5Util.md5(pwd));
//        String salt = MD5Util.getSalt();
//        System.out.println(salt);
//        System.out.println(MD5Util.md5(pwd+salt));
        return new Result().ok();
    }

    @RequestMapping("test2.do")
    public Object test2(String token){
        //JwtUtil.checkJwt(token);
        return new Result<>("200","token验证成功");
    }

    @RequestMapping("/upload.do")
    public Object upload(MultipartFile file, HttpServletRequest request){
        //获取项目路径
        String realPath =  request.getServletContext().getRealPath("images");
        System.out.println(realPath);
        //获取文件名
        String name = file.getOriginalFilename();
        System.out.println(name);
        File f = new File(realPath+File.separator+name);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return new Result().ok();
    }

}
